package au.com.iglootech.TaxTimeToolTip.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jdo.JdoCallback;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.appengine.api.datastore.Key;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.Task;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.resource.ConstantResource;
import au.com.iglootech.TaxTimeToolTip.service.TaskService;
import au.com.iglootech.TaxTimeToolTip.service.TaxTimeSheetLogService;
import au.com.iglootech.TaxTimeToolTip.util.DateUtils;
@Component(TaskServiceImpl.BEANNAME)
@Transactional
@SuppressWarnings("unchecked")
public class TaskServiceImpl implements TaskService {

	public static final String BEANNAME = "TaskServiceImpl";

	@Autowired
	private transient GenericDao genericDao;
	
	@Override
	public Task createTask(Task task) {
		Task instance = genericDao.save(task);
		return instance;
	}

	@Override
	public List<Task> findAll() {
		return new ArrayList<Task>(genericDao.findAll(Task.class));
	}

	@Override
	public Task findById(Key key) {
		return genericDao.findByKey(Task.class, key);
	}

	@Override
	public List<Task> findTodayTasks() {
		return new ArrayList<Task>(genericDao.getJdoTemplate().execute(
				new JdoCallback<Collection<Task>>() {
					public Collection<Task> doInJdo(PersistenceManager pm)
							throws JDOException {
						Query query = pm.newQuery(Task.class);
						query.setFilter("startDate <= pEndDate && startDate >= pStartDate");
						query.declareImports("import java.util.Date");  
						query.declareParameters("Date pEndDate, Date pStartDate");
						return pm.detachCopyAll((Collection<Task>) query.executeWithArray(DateUtils.getEndOfDate(new Date()), DateUtils.getBeginOfDate(new Date())));
					}
				}));
	}

	@Override
	public void remove(Task task) {
		genericDao.remove(task);
	}

	@Override
	public Boolean updateTask(Task task) {
		genericDao.save(task);
		return null;
	}

	@Override
	public List<Task> findTaskByCompany(final Company company, final Date startDate,
			final Date endDate, final Boolean isInvoiced) {
		return new ArrayList<Task>(genericDao.getJdoTemplate().execute(
				new JdoCallback<Collection<Task>>() {
					public Collection<Task> doInJdo(PersistenceManager pm)
							throws JDOException {
						Query query = pm.newQuery(Task.class);
						if(isInvoiced == null)
						{
							// all
							query.setFilter("company == pCompany && startDate >= pStartDate && startDate <= pEndDate");
							query.declareImports("import java.util.Date");  
							query.declareParameters("Company pCompany, Date pStartDate, Date pEndDate");
							Date pStartDate = startDate == null ? DateUtils.systemStartDate() : startDate;
							Date pEndDate = endDate == null ? DateUtils.systemEndDate() : endDate;							
							return pm.detachCopyAll((Collection<Task>) query.executeWithArray(company, pStartDate, pEndDate));
						}
						else
						{
							query.setFilter("company == pCompany && startDate >= pStartDate && startDate <= pEndDate && invoiced == pInvoiced");
							query.declareImports("import java.util.Date");  
							query.declareParameters("Company pCompany, Date pStartDate, Date pEndDate, Boolean pInvoiced");
							Date pStartDate = startDate == null ? DateUtils.systemStartDate() : startDate;
							Date pEndDate = endDate == null ? DateUtils.systemEndDate() : endDate;						
							return pm.detachCopyAll((Collection<Task>) query.executeWithArray(company, pStartDate, pEndDate, isInvoiced));
						}
						
					}
				}));
	}

}
